package com.wanhe.base.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Random;

@SuppressLint("DefaultLocale")
public class DeviceUtil {

	public static int screenWidth, screenHeight;

	private static final String TAG = "DeviceUtil";

	private static int OrignalScreenHeight = 0;

	public static boolean isSDcardEnabel() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	/**
	 * SD卡的要目录
	 *
	 * @return
	 */
	public static String getSDCardRootPath() {
		boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			File sdDir = Environment.getExternalStorageDirectory();
			return sdDir.toString();
		} else {
			return null;
		}
	}

	/**
	 * 根据系统版本获取sd卡保存的目录
	 *
	 * @return
	 */
	public static String getSDCardPath(Context mContext) {
		boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			if (!hasKitka() || mContext == null) {
				File sdDir = Environment.getExternalStorageDirectory();
				return sdDir.toString();
			} else {
				// 2.在android4.4以上的版本启用新目录
				//Android/data/包名/file/,不易被清理助手清除
				return mContext.getExternalFilesDir(null).toString();
			}
		}
		return null;
	}

	/**
	 * sd卡是否有足够的可以空间
	 *
	 * @return boolean
	 */
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static boolean isSdCardAvailale(long maxSize) {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			long size = 0;
			StatFs fs = new StatFs(Environment.getExternalStorageDirectory().getPath());
			//Android API18之前：fs.getAvailableBlocks()*fs.getBlockSize()
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
				size = fs.getAvailableBytes() / 1024;
			} else {
				File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
				StatFs stat = new StatFs(path.getPath());
				long blockSize = stat.getBlockSize();
				long availableBlocks = stat.getAvailableBlocks();
				size = (availableBlocks * blockSize) / 1024;
			}
			if (size < maxSize) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}

	public static boolean isSdCardAvailale() {
		return isSdCardAvailale(500);
	}

	public static String getDeviceIDForServer(Context mContext) {
		// step1: get IMEI
		String strID = getIMEI(mContext);

		// step2: if IMEI null, get MAC address
		if (TextUtils.isEmpty(strID)) {
			strID = getMAC(mContext);
		}

		// step3: if MAC address null, get Android ID
		if (TextUtils.isEmpty(strID)) {
			strID = getAndroidID(mContext);
		}

		return strID;
	}

	public static String getIMEI(Context mContext) {
		try {
			TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
					Context.TELEPHONY_SERVICE);
			return tm.getDeviceId();
		} catch (Exception e) {
			return "";
		}
	}

	public static String getMAC(Context mContext) {
		WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	public static String getAndroidID(Context mContext) {
		return Settings.System
				.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
	}

	/**
	 * 获取软件版本名称
	 *
	 * @return
	 */
	public static String getVersionName(Context mContext) {
		String versionName = "";
		try {
			PackageInfo packageinfo = mContext.getPackageManager().getPackageInfo(
					mContext.getPackageName(), PackageManager.GET_INSTRUMENTATION);
			versionName = packageinfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}


	public static String getSystemLanguage() {
		return Locale.getDefault().getLanguage();
	}

	public static String getSystemCountry() {
		return Locale.getDefault().getCountry();
	}

	public static String getRegion() {
		return Locale.getDefault().getCountry().toString();
	}

	/**
	 * network tools
	 */
	public static boolean isWifiEnable(Context mContext) {
		WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		return wifiManager.isWifiEnabled();
	}

	public static String getActiveNetWorkName(Context mContext) {
		ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		String result = null;
		do {
			if (connectivity == null) {
				break;
			}

			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {

					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						result = info[i].getTypeName();
					}
				}
			}
		} while (false);
		return result;
	}

	static private String intToIp(int i) {
		String strIp = (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
		return strIp;
	}

	public static String GprsgetLocalIpAddress() {
		String strFirstIp = null;

		try {

			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
				NetworkInterface ni = en.nextElement();
				/*
				 * System.out.println("****displayname: " +
				 * ni.getDisplayName());
				 * System.out.println("****name: " + ni.getName());
				 * System.out.println("****MTU: " + ni.getMTU());
				 * System.out.println("****Loopback: " + ni.isLoopback());
				 * System.out.println("****Virtual: " + ni.isVirtual());
				 * System.out.println("****Up: " + ni.isUp());
				 * System.out.println("****PointToPoint: " +
				 * ni.isPointToPoint());
				 */

				boolean bFirst = true;
				for (Enumeration<InetAddress> enumIpAddr = ni.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {

					InetAddress inetAddress = enumIpAddr.nextElement();

					if (!inetAddress.isLoopbackAddress()) {

						/*
						 * //System.out.println(
						 * " ----------- getCanonicalHostName: "+
						 * inetAddress.getCanonicalHostName());
						 * System.out.println( " ----------- getHostName: "+
						 * inetAddress.getHostName());
						 * System.out.println( " ----------- getHostAddress: "+
						 * inetAddress.getHostAddress());
						 * System.out.println( " ----------- hashCode: "+
						 * inetAddress.hashCode());
						 * System.out.println( " ----------- toString: "+
						 * inetAddress.toString());
						 * System.out.println(
						 * " ----------- isLinkLocalAddress: "+
						 * inetAddress.isLinkLocalAddress());
						 */

						strFirstIp = inetAddress.getHostAddress().toString();
						if (bFirst) {
							bFirst = false;
						} else {
							// return strFirstIp;
						}
					}
				}
			}
		} catch (SocketException ex) {
			// Log.e("WifiPreference IpAddress", ex.toString());
		} catch (Exception e) {
			// Log.e("WifiPreference IpAddress", e.toString());
		}
		return strFirstIp;
	}

	public static String GetWifiIp(Context mContext) {
		WifiManager wifimanage = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);// 获取WifiManager
		WifiInfo wifiinfo = wifimanage.getConnectionInfo();
		String ip = intToIp(wifiinfo.getIpAddress());
		return ip;
	}

	public static int dip2px(Context mContext, float dipValue) {
		return (int) (dipValue * getDensity(mContext) + 0.5f);
	}

	public static double dp2px(Context mContext, float dipValue) {
		return (dipValue * getDensity(mContext) + 0.5f);
	}

	public static int px2dip(Context mContext, float pxValue) {
		return (int) (pxValue / getDensity(mContext) + 0.5f);
	}

	public static float getDensity(Context mContext) {
		DisplayMetrics metrics = getDisplayMetrics(mContext);
		return metrics.density;
	}

	public static DisplayMetrics getDisplayMetrics(Context mContext) {
		return mContext.getResources().getDisplayMetrics();
	}

	/**
	 * return min size of screen
	 *
	 * @return
	 */
	public static int getScreenMinPix(Context mContext) {
		int W = getScreenWidth(mContext);
		int H = getScreenHeight(mContext);
		return W < H ? W : H;
	}

	/**
	 * return width of screen, with px
	 *
	 * @return int
	 */
	public static int getScreenWidth(Context mContext) {
		if (screenWidth == 0) {
			DisplayMetrics metrics = getDisplayMetrics(mContext);
			screenWidth = metrics.widthPixels;
		}
		return screenWidth;
	}

	/**
	 * return height size of screen, with px
	 *
	 * @return int
	 */
	public static int getScreenHeight(Context mContext) {
		if (screenHeight == 0) {
			DisplayMetrics metrics = getDisplayMetrics(mContext);
			screenHeight = metrics.heightPixels;
		}
		return screenHeight;
	}

	public static int getOrignalHeight(Activity activity) {
		if (OrignalScreenHeight != 0) {
			return OrignalScreenHeight;
		}

		Display display = activity.getWindowManager().getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		@SuppressWarnings("rawtypes")
		Class c;
		try {
			c = Class.forName("android.view.Display");
			@SuppressWarnings("unchecked")
			Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
			method.invoke(display, dm);
			OrignalScreenHeight = dm.heightPixels;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return OrignalScreenHeight;
	}

	/**
	 * 获得屏幕分辨率
	 *
	 * @return String
	 */
	public static String getScreenResolution(Context mContext) {
		int w = getScreenWidth(mContext);
		int h = getScreenHeight(mContext);
		String resolution = "";
		if (w <= h) {
			resolution = w + "*" + h;
		} else {
			resolution = h + "*" + w;
		}
		return resolution;
	}

	/**
	 * 设置成全屏
	 *
	 * @param activity void
	 */
	public static void setFullScreen(Activity activity) {
		if (activity == null) {
			return;
		}
		WindowManager.LayoutParams params = activity.getWindow().getAttributes();
		params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
		activity.getWindow().setAttributes(params);
		activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}

	/**
	 * 设置成非全屏，显示状态栏
	 *
	 * @param activity void
	 */
	public static void setNoFullScreen(Activity activity) {
		if (activity == null) {
			return;
		}
		WindowManager.LayoutParams params = activity.getWindow().getAttributes();
		params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		activity.getWindow().setAttributes(params);
		activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}

	/**
	 * 获得手机状态栏的高度
	 *
	 * @return int
	 */
	@SuppressWarnings("rawtypes")
	public static int getStatusBarHeigh(Context mContext) {
		int heigh = 0;
		try {

			Class c = Class.forName("com.android.internal.R$dimen");
			Object obj = c.newInstance();
			Field field = c.getField("status_bar_height");
			int x = Integer.parseInt(field.get(obj).toString());
			heigh = mContext.getResources().getDimensionPixelSize(x);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return heigh;
	}

	/**
	 * get the SoftInput state
	 *
	 * @param activity
	 * @return if shown return true else return false
	 */
	public static boolean softInputIsShow(Activity activity) {
		if (activity == null) {
			return false;
		}
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		return imm.isActive();
	}

	/**
	 * open the SoftInput
	 *
	 * @param activity
	 * @param mEditText open for this EditText
	 */
	public static void showSoftInput(Activity activity, EditText mEditText) {
		if (activity == null) {
			return;
		}
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);
		if (imm != null) {
			imm.toggleSoftInputFromWindow(mEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

		}
	}

	public static void closeSoftInput(Activity activity) {
		if (activity == null) {
			return;
		}
		try {
			InputMethodManager inputMethodManager = (InputMethodManager) activity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (inputMethodManager != null && inputMethodManager.isActive()) {
				inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
			}
		} catch (Exception e) {

		}
	}

	/**
	 * close the SoftInput
	 *
	 * @param activity
	 */
	public static void closeSoftInput(Activity activity, EditText mEditText) {
		if (activity == null || mEditText == null) {
			return;
		}

		InputMethodManager inputMethodManager = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null && inputMethodManager.isActive()) {
			inputMethodManager.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);

		}
	}

	public static boolean isSoftInputActive(Activity activity, EditText mEditText) {
		if (activity == null) {
			return false;
		}
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		// imm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED);
		if (imm != null) {
			return imm.isActive();
		}
		return false;
	}

	public static void openImm(Activity context) {
		if (context == null) {
			return;
		}
		View v = context.getWindow().peekDecorView();
		if (v != null && v.getWindowToken() != null) {
			InputMethodManager imm = (InputMethodManager) context
					.getSystemService(context.INPUT_METHOD_SERVICE);
			// 如果输入法打开则关闭，如果没打开则打开
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
		}
	}

	public static boolean hasFroyo() {
		// Can use static final constants like FROYO, declared in later versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	}

	public static boolean hasGingerBread() {
		// Can use static final constants like FROYO, declared in later versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
	}

	public static boolean hasGingerBreadMR1() {
		// Can use static final constants like FROYO, declared in later versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1;
	}

	public static boolean hasHoneycomb() {
		// Can use static final constants like HONEYCOMB, declared in later
		// versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
	}

	public static boolean hasJellybean() {
		// Can use static final constants like JELLY_BEAN, declared in later
		// versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
	}

	/**
	 * 判斷目前版本是否為Kitkat(4.4)以上
	 *
	 * @return
	 * @author Ricky Chen
	 * 2015年3月9日 下午6:11:03
	 */
	public static boolean hasKitkat() {
		return Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH;
	}

	public static boolean hasJellyBeanMr1() {
		//api-17
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
	}

	public static boolean hasKitka() {
		//api-19
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
	}

	/**
	 * 随机产生16位的数字，测试使用
	 *
	 * @return
	 */
	public static byte[] getRandomDeviceID16ByTest() {
		char[] chPas = new char[62];
		char c1 = 'a';
		char c2 = 'A';
		char c3 = '0';
		for (int i = 0; i < chPas.length; i++) {
			if (i < 26) {
				chPas[i] = c1++;
			}
			if (i >= 26 && i < 52) {
				chPas[i] = c2++;
			} else {
				chPas[i] = c3++;
			}
		}
		Random rand = new Random();
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < 16; i++) {
			str.append(chPas[rand.nextInt(62)]);
		}
		return str.toString().getBytes();
	}

	public static byte[] deviceID16(Context mContext) {
		String dId = getDeviceIDForServer(mContext);
		if (dId == null) {
			return null;
		}
		byte chOld[] = dId.getBytes();
		byte chNew[] = new byte[16];
		for (int i = 0; i < 16; i++) {
			int n1 = -1, n2 = -1;
			if (i * 2 <= chOld.length - 1) {
				byte ch1 = chOld[i * 2];
				n1 = convertCharacter(ch1);
			}

			if (i * 2 + 1 <= chOld.length - 1) {
				byte ch2 = chOld[i * 2 + 1];
				n2 = convertCharacter(ch2);
			}

			int nAnd = ((n1 < 0) ? 0 : (n1 << 4)) + ((n2 < 0) ? 0 : n2);
			chNew[i] = (byte) nAnd;
		}
		return chNew;
	}

	public static String getDeviceID(Context mContext) {
		String imei = DeviceUtil.getDeviceIDForServer(mContext);
		if (TextUtils.isEmpty(imei)) {
			return "";
		}
		for (int i = imei.length(); i < 32; i++) {
			imei = imei + "0";
		}
		imei = imei.replace(':', '0');
		return imei;
	}

	public static int convertCharacter(byte chac) {
		int nAs = -1;
		if (chac >= '0' && chac <= '9') {
			nAs = 0 + (chac - '0');
		} else if (chac >= 'a' && chac <= 'f') {
			nAs = 0xa + (chac - 'a');
		} else if (chac >= 'A' && chac <= 'F') {
			nAs = 0xa + (chac - 'A');
		}

		return nAs;
	}

	public static long getAvailableStorage() {

		String storageDirectory = null;
		storageDirectory = Environment.getExternalStorageDirectory().toString();

		try {
			StatFs stat = new StatFs(storageDirectory);
			long avaliableSize = ((long) stat.getAvailableBlocks() * (long)
					stat.getBlockSize());
			return avaliableSize;
		} catch (RuntimeException ex) {
			return 0;
		}
	}

	@SuppressLint("DefaultLocale")
	public static String getPhoneName() {
		String str = Build.MODEL;
		if (!TextUtils.isEmpty(str)) {
			String band = Build.BRAND;
			if (str.toLowerCase().indexOf(band.toLowerCase()) == -1) {
				str = band + " " + str;
			}
			return str;
		} else {
			return "";
		}
	}

	public static boolean isNetworkOnline(Context mContext) {
		ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(
				Context.CONNECTIVITY_SERVICE);

		NetworkInfo ni = connectivity.getActiveNetworkInfo();
		// return ni != null && ni.isConnectedOrConnecting();
		if (ni != null && ni.isConnected()) {
			return true;
		} else {
			return false;
		}
	}

	public static final int NETWORKTYPE_INVALID = 0;
	/**
	 * wap网络
	 */
	public static final int NETWORKTYPE_WAP = 1;
	/**
	 * 2G网络
	 */
	public static final int NETWORKTYPE_2G = 2;
	/**
	 * 3G和3G以上网络，或统称为快速网络
	 */
	public static final int NETWORKTYPE_3G = 3;
	/**
	 * wifi网络
	 */
	public static final int NETWORKTYPE_WIFI = 4;

	public static final int NETWORKTYPE_ETHERNET = 5;

	public static int getNetWorkType_woo(Context mContext) {

		ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();

		int mNetWorkType = 0;
		if (networkInfo != null && networkInfo.isConnected()) {

			String type = networkInfo.getTypeName();
			Logger.e("DeviceUtil", "  ==== " + networkInfo.toString());

			// GprsgetLocalIpAddress();
			if (type.equalsIgnoreCase("WIFI")) {
				mNetWorkType = NETWORKTYPE_WIFI;

				GetWifiIp(mContext);
			} else if (type.equalsIgnoreCase("MOBILE")) {

				Logger.e("DeviceUtil", "GPRS IP" + GprsgetLocalIpAddress());
				// String proxyHost = android.net.Proxy.getDefaultHost();
				// mNetWorkType =
				// TextUtils.isEmpty(proxyHost)?(isFastMobileNetwork_woo(context)
				// ? NETWORKTYPE_3G : NETWORKTYPE_2G): NETWORKTYPE_WAP;
				mNetWorkType = isFastMobileNetwork_woo(mContext) ? NETWORKTYPE_3G : NETWORKTYPE_2G;
			} else {
				mNetWorkType = NETWORKTYPE_ETHERNET;
				Logger.e("DeviceUtil", "=== begin other  Net Type Ip:");
				GetWifiIp(mContext);
				Logger.e("DeviceUtil", "GPRS IP" + GprsgetLocalIpAddress());
				Logger.e("DeviceUtil", "=== End other  Net Type Ip");
			}

		} else {

			mNetWorkType = NETWORKTYPE_INVALID;

		}

		return mNetWorkType;
	}

	/**
	 * 获得网络的类型，如果为 0，则表示无网络
	 *
	 * @return int
	 * @throws SocketException
	 */

	//	public static P2PNetInfo getNetWorkTypeIP(Context context) {
	//
	//		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	//		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
	//		P2PNetInfo node = new P2PNetInfo();
	//
	//		int mNetWorkType = 0;
	//		if (networkInfo != null && networkInfo.isConnected()) {
	//
	//			String type = networkInfo.getTypeName();
	//			Logger.e("DeviceUtil","  ==== " + networkInfo.toString());
	//
	//			// GprsgetLocalIpAddress();
	//			if (type.equalsIgnoreCase("WIFI")) {
	//				mNetWorkType = NETWORKTYPE_WIFI;
	//
	//				node.nType = 4;
	//				node.strIP = GetWifiIp();
	//			} else if (type.equalsIgnoreCase("MOBILE")) {
	//
	//				node.strIP = GprsgetLocalIpAddress();
	//				Logger.e("DeviceUtil","GPRS IP" + GprsgetLocalIpAddress());
	//				mNetWorkType = isFastMobileNetwork_woo(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G;
	//				node.nType = (mNetWorkType == NETWORKTYPE_2G) ? 1 : 2;
	//			}
	//			else
	//			{
	//				mNetWorkType = NETWORKTYPE_ETHERNET;
	//
	//			}
	//
	//		} else {
	//
	//			mNetWorkType = NETWORKTYPE_INVALID;
	//
	//		}
	//
	//		return node;
	//	}
	private static boolean isFastMobileNetwork_woo(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

		switch (telephonyManager.getNetworkType()) {

			case TelephonyManager.NETWORK_TYPE_1xRTT:
				return false; // ~ 50-100 kbps
			case TelephonyManager.NETWORK_TYPE_CDMA:
				return false; // ~ 14-64 kbps

			case TelephonyManager.NETWORK_TYPE_EDGE:
				return false; // ~ 50-100 kbps

			case TelephonyManager.NETWORK_TYPE_EVDO_0:
				return true; // ~ 400-1000 kbps

			case TelephonyManager.NETWORK_TYPE_EVDO_A:
				return true; // ~ 600-1400 kbps

			case TelephonyManager.NETWORK_TYPE_GPRS:
				return false; // ~ 100 kbps

			case TelephonyManager.NETWORK_TYPE_HSDPA:
				return true; // ~ 2-14 Mbps

			case TelephonyManager.NETWORK_TYPE_HSPA:
				return true; // ~ 700-1700 kbps

			case TelephonyManager.NETWORK_TYPE_HSUPA:
				return true; // ~ 1-23 Mbps

			case TelephonyManager.NETWORK_TYPE_UMTS:
				return true; // ~ 400-7000 kbps

			case TelephonyManager.NETWORK_TYPE_EHRPD:
				return true; // ~ 1-2 Mbps

			case TelephonyManager.NETWORK_TYPE_EVDO_B:
				return true; // ~ 5 Mbps

			case TelephonyManager.NETWORK_TYPE_HSPAP:
				return true; // ~ 10-20 Mbps

			case TelephonyManager.NETWORK_TYPE_IDEN:
				return false; // ~25 kbps

			case TelephonyManager.NETWORK_TYPE_LTE:
				return true; // ~ 10+ Mbps

			case TelephonyManager.NETWORK_TYPE_UNKNOWN:
				return false;

			default:
				return false;
		}
	}


	public static String GetNetTypeString(int nNetType) {
		String strType = "";
		switch (nNetType) {
			case NETWORKTYPE_INVALID:
				strType = "未知网络;";
				break;
			case NETWORKTYPE_WAP:
				strType = "Wap 网络";
				break;

			case NETWORKTYPE_2G:
				strType = "2G 网络";
				break;

			case NETWORKTYPE_3G:
				strType = "3G和3G以上网络";
				break;
			case NETWORKTYPE_WIFI:
				strType = "wifi网络";
				break;
			default:
				break;
		}

		return strType;
	}

	/**
	 * 判断是否有摄像头
	 *
	 * @return
	 */
	public static boolean hasCamera(Context mContext) {
		if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
			return true;
		} else {
			return false;
		}
	}


	// 获取CPU名字
	public static String getCpuName() {
		try {
			FileReader fr = new FileReader("/proc/cpuinfo");
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(fr);
			String text = br.readLine();
			String[] array = text.split(":\\s+", 2);
			for (int i = 0; i < array.length; i++) {
			}
			return array[1];
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *
	 * [获取cpu类型和架构]
	 *
	 * @return
	 * 三个参数类型的数组，第一个参数标识是不是ARM架构，第二个参数标识是V6还是V7架构，第三个参数标识是不是neon指令集
	 */
/*	public static Object[] getCpuArchitecture() {
		if ((Integer) mArmArchitecture[1] != -1) {
			return mArmArchitecture;
		}
		try {
			InputStream is = new FileInputStream("/proc/cpuinfo");
			InputStreamReader ir = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(ir);
			try {
				String nameProcessor = "Processor";
				String nameFeatures = "Features";
				String nameModel = "model name";
				String nameCpuFamily = "cpu family";
				while (true) {
					String line = br.readLine();
					String[] pair = null;
					if (line == null) {
						break;
					}
					pair = line.split(":");
					if (pair.length != 2)
						continue;
					String key = pair[0].trim();
					String val = pair[1].trim();
					if (key.compareTo(nameProcessor) == 0) {
						String n = "";
						for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {
							String temp = val.charAt(i) + "";
							if (temp.matches("\\d")) {
								n += temp;
							} else {
								break;
							}
						}
						mArmArchitecture[0] = "ARM";
						mArmArchitecture[1] = Integer.parseInt(n);
						continue;
					}

					if (key.compareToIgnoreCase(nameFeatures) == 0) {
						if (val.contains("neon")) {
							mArmArchitecture[2] = "neon";
						}
						continue;
					}

					if (key.compareToIgnoreCase(nameModel) == 0) {
						if (val.contains("Intel")) {
							mArmArchitecture[0] = "INTEL";
							mArmArchitecture[2] = "atom";
						}
						continue;
					}

					if (key.compareToIgnoreCase(nameCpuFamily) == 0) {
						mArmArchitecture[1] = Integer.parseInt(val);
						continue;
					}
				}
			} finally {
				br.close();
				ir.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mArmArchitecture;
	}*/
}
